// pages/mymocaibi/mymocaibi.js
var utils = require('../../utils/api.js');
var base64 = require('../../utils/base64.js');
var md5 = require('../../utils/md5.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getList: [], //列表数组
    getActive: 'active',
    consumeActive: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getMoroCoinRecordReq()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 魔彩币获取记录
   */
  getMoroCoinRecordReq: function () {
    var that = this
    wx.request({
      url: getApp().globalData.requestUrl + '/getAcquireInfo',
      method: "post",
      data: {
        paramList: base64.CusBASE64.encoder('{"serverSessionKey":"' + wx.getStorageSync('serverSessionKey') + '"}')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.statusCode == 200) { //判断wx.request请求是否成功
          if (res.data) {
            var dat = utils.stringToJson(utils.ascii2native(res.data))
            console.log('interface:getAcquireInfo')
            console.log(dat)
            if (dat.status == 0) {
              if (dat.dataList) {
                // 数据赋值
                var param = new Array()
                for (var i = 0; i < dat.dataList.length; i++) {
                  param[i] = ['购买' + dat.dataList[i].buyCount + '张' + dat.dataList[i].lotteryPrice + '元有礼了即开彩票，获得' + dat.dataList[i].moroCoinCount + '个魔彩币', dat.dataList[i].getTime]
                }
                that.setData({ getList: param })
                // 隐藏信息
                wx.hideLoading()
                that.setData({ allLoading: true })
              } else { //获取信息错误
                that.CommonShowMsg('数据错误')
              }
            } else {
              that.CommonShowMsg(dat.errorMsg)
            }
          } else {//服务器没有返回数据
            that.CommonShowMsg('数据错误')
          }
        } else {//请求失败
          that.CommonShowMsg('数据错误')
        }
      },
      fail: function (res) {
      },
      complete: function () {
      },
    })
  },
  /**
   * 魔彩币消费记录
   */
  consumeMoroCoinRecordReq: function () {
    var that = this
    wx.request({
      url: getApp().globalData.requestUrl + '/getConsumptionInfo',
      method: "post",
      data: {
        paramList: base64.CusBASE64.encoder('{"serverSessionKey":"' + wx.getStorageSync('serverSessionKey') + '"}')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.statusCode == 200) { //判断wx.request请求是否成功
          if (res.data) {
            var dat = utils.stringToJson(utils.ascii2native(res.data))
            console.log('interface:getConsumptionInfo')
            console.log(dat)
            if (dat.status == 0) {
              if (dat.dataList) {
                // 数据赋值
                var param = new Array()
                for (var i = 0; i < dat.dataList.length; i++) {
                  param[i] = [dat.dataList[i].goodsName + '消费' + dat.dataList[i].number + '个魔彩币', dat.dataList[i].tradeTime]
                }
                that.setData({ getList: param })
              } else { //获取信息错误
                that.CommonShowMsg('数据错误')
              }
            } else {
              that.CommonShowMsg(dat.errorMsg)
            }
          } else {//服务器没有返回数据
            that.CommonShowMsg('数据错误')
          }
        } else {//请求失败
          that.CommonShowMsg('数据错误')
        }
      },
      fail: function (res) {
      },
      complete: function () {
      },
    })
  },
  /**
   * 获得切换
   */
  getTab: function () {
    var that = this
    that.setData({
      getActive: 'active',
      consumeActive: '',
      getList: [],
    })
    that.getMoroCoinRecordReq()
  },
  /**
   * 消费切换
   */
  consumeTab: function () {
    var that = this
    that.setData({
      getActive: '',
      consumeActive: 'active',
      getList: [],
    })
    that.consumeMoroCoinRecordReq()
  }
})